/*
 * PortScan.java
 *
 * Created on 25 febbraio 2004, 17.28
 */

import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import java.io.*;

/**
 *
 * @author  Andrea 'Kl1mT' Fortuna
 * @version 0.0.1
 */

public class PortScan implements Runnable, CommandListener {
    
private Command backCommand;
private MobilePortScannerMIDlet parent;
private Display display;
private Form f;
private StringItem si;
private SocketConnection sc;
private HttpConnection hc;
//private UDPDatagramConnection dc;
private InputStream is;
private String hostname;
private Ticker tkStatus;
private Gauge gProgress;
private String sResult;

public PortScan(MobilePortScannerMIDlet m, String hostname) {
parent = m;
this.hostname = hostname;
display = Display.getDisplay(parent);
backCommand = new Command("Back", Command.BACK,1);
tkStatus = new Ticker("Scanning host "+hostname);
gProgress = new Gauge("Progress..",false,1,1);

f = new Form("MobilePortScanner");
f.setTicker(tkStatus);
f.append(gProgress);
f.append("Host "+hostname+":\n\n");
f.addCommand(backCommand);
f.setCommandListener(this);
display.setCurrent(f);
}

public void start() {
Thread t = new Thread(this);
t.start();
}

public void run() {

    int[] ports = {21,22,23,25,37,42,79,80,110,135,139,143,443,513,8080};
    
    String protocol = new String();

    gProgress.setMaxValue(ports.length);
    sResult = new String ("Host " + hostname + ":\n");
for (int i = 0; i < ports.length; i++) {
   //protocol = "datagram://";
   //protocol = "http://";
    gProgress.setValue(i+1);
    try {
   
        if (ports[i]==80 || ports[i]==8080 || ports[i]==443) {
            protocol = "http://";
            hc = (HttpConnection) Connector.open(protocol+hostname+":"+ports[i],1,true);
            is = hc.openInputStream();
        } else {
            protocol = "socket://";
            //sc = (SocketConnection) Connector.open(protocol+hostname+":"+ports[i]);
            sc = (SocketConnection) Connector.open(protocol+hostname+":"+ports[i],3,true);
            //sc.setSocketOption(DELAY, 0);
            //sc.setSocketOption(KEEPALIVE, 0);
            is = sc.openInputStream();
        }
        
        //hc = (HttpConnection) Connector.open(protocol+hostname+":"+ports[i]);
        //sc = (SocketConnection) Connector.open(protocol+hostname+":"+ports[i]);
        //is = sc.openInputStream();
        //is = hc.openInputStream();
        //is = dc.openInputStream();
        is.close();
        f.append("Port " + ports[i] + " is OPEN\n");

    } catch(IOException e) {
        f.append("Port "+ports[i]+" is CLOSED\n");
        //sResult.concat("Port "+ports[i]+" is CLOSED\n");
    } finally {
       try {
            if(is != null) {
                is.close();
            }
            if(sc != null) {
                sc.close();
            }
           } catch(IOException e) {
                e.printStackTrace();
            }
    }
    gProgress.setValue(i+1);
}
 
    tkStatus.setString("Scanning finished...");
    f.delete(0);
    //f.append(sResult);
}
public void commandAction(Command c, Displayable s) {
if (c == backCommand) {
    display.setCurrent(parent.form_main);
}
}
}

